{%extends file='layout/layout.html' %}
{%block name="content"%}
<div class="hastable">

    <div style="margin-bottom: 10px">

        <label class="btn btn-success btn-sm" onclick=goback("/admin/downuser")><i class="fa fa-download"></i>批量导出</label>
    </div>

    <div style="margin-bottom: 10px">
        <label style="margin-right: 10px">手机/姓名:</label><input style="margin-right: 10px" type="text" name="search" value="{%$search|default:''%}">
        <label style="margin-right: 10px">生日:</label><input readonly class="datepicker" style="margin-right: 10px;width: 100px" type="text" value="{%$starttime|default:''%}" name="starttime"><label style="margin-right: 10px">~</label><input readonly class="datepicker" value="{%$endtime|default:''%}" style="margin-right: 10px;width: 100px" type="text" name="endtime">
        <label class="btn btn-success btn-sm" onclick="doserch()"><i class="fa fa-search"></i>检索</label>
        <label class="btn btn-success btn-sm" onclick="reset()"><i class="fa fa-remove"></i>清空</label>
    </div>

    <table>

        <thead>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>手机号</th>
            <th>生日</th>
            <th>性别</th>
            <th>地区</th>
            <th>积分</th>
            <th style="width:132px">操作</th>
        </tr>
        </thead>
        <tbody>
        {%if $data|@count neq 0 %}
        {%foreach $data as $key=>$value%}
        <tr class="{%if $key%2 neq 0 %}even{%else%}odd{%/if%}">
            <td class="center">{%$key+1%}</td>
            <td>{%$value.username|default:''%}</td>
            <td>{%$value.mobile|default:''%}</td>
            <td>{%$value.birthday|date_format:'%Y-%m-%d'|default:''%}</td>
            <td>{%$value.sex|default:''%}</td>
            <td>{%$value.region|default:''%}-{%$value.city|default:''%}-{%$value.area|default:''%}</td>
            <td width="150">{%$value.intergtal|default:''%}</td>
            <td>
                <a onclick="editrow(this)" class="btn_no_text btn ui-state-default ui-corner-all tooltip" href="javascript:void(0)">
                    <span class="ui-icon ui-icon-wrench"></span>
                </a>
                <label attrid="{%$value.uid|default:''%}" onclick="saverow(this)" style="display: none" class="btn btn-success btn-sm "><i class="fa fa-save"></i>保存</label>
            </td>
        </tr>
        {%/foreach%}
        {%else%}
        <tr class="even">
            <td colspan="8" style="text-align: center">无内容</td>
        </tr>
        {%/if%}

        </tbody>

    </table>

    <ul style="width:50%;margin: 0 auto;text-align: center" id="pagination"></ul>

</div>
<div class="clearfix"></div>

{%/block%}
{%block name="js" append%}
<script type="text/javascript">

    $(function () {
        init()
    })
    function init() {
        $(".datepicker").datepicker({
            dateFormat: "yy-mm-dd",
            showOtherMonths: true,
            selectOtherMonths: true,
            showButtonPanel: true,
            showOn: "both",
            buttonImageOnly: true,
            buttonText: "",
            changeMonth: true,
            changeYear: true
        });
        {%if $data|@count neq 0 %}

        $("#pagination").twbsPagination("destroy");
        $('#pagination').twbsPagination({
            startPage: parseInt("{%$page|default:1%}"),
            initiateStartPageClick: false,
            totalPages: parseInt("{%$total%}"),
            visiblePages: 10,
            first: '首页',
            last: '尾页',
            prev: '«',
            next: '»',
            paginationClass: '',
            nextClass: 'next',
            prevClass: '',
            lastClass: '',
            firstClass: '',
            pageClass: '',
            activeClass: 'active',
            disabledClass: 'previous-off',
            anchorClass: '',
            onPageClick: function (event, pager) {

                var data = {page: parseInt("{%$page|default:1%}")};
                window.location.href = '/admin/user/' + $.base64.btoa(JSON.stringify(data));

            }

        });
        {%/if%}
    }
    function doserch() {
        var search = $("input[name='search']").val();
        var starttime = $("input[name='starttime']").val();
        var endtime = $("input[name='endtime']").val();
        var data = {
            search: escape($.trim(search)),
            starttime: starttime,
            endtime: endtime,
            page: parseInt("{%$page|default:1%}")
        };

    window.location.href = '/admin/user/' + $.base64.btoa(JSON.stringify(data));

    }
    function reset() {
        $("input[name='search']").val('');
        $("input[name='starttime']").val('');
        $("input[name='endtime']").val('');
        window.location.href = '/admin/user';
    }
    function editrow(obj) {
        var tr=obj.parentNode.parentNode;
        tr.cells[6].innerHTML="<input style='width:80px' value='"+tr.cells[6].innerText+"'>";
        obj.style.display="none";
        obj.nextElementSibling.style.display="inline-block";

    }
    function saverow(obj) {
        var tr=obj.parentNode.parentNode;
        var uid=obj.getAttribute('attrid');
        var intergtal=tr.cells[6].querySelector("input").value;

        $.post("/api/saveuser.json",{uid:uid,intergtal:intergtal,tk_name:"{%$tk_name%}",tk_value:"{%$tk_value%}"},function (data, textStatus, jqXHR) {
            if(jqXHR.status==200){
                showmsg(jqXHR.responseText, 1);
                dorefulsh();
            }else{
                showmsg(jqXHR.responseText, 2);
            }
        });


    }


</script>
{%/block%}
